package com.qianbao.qianbaobusiness.ui.cashier;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidbase.base.BasePopupWindow;
import com.androidbase.utils.ToastUtils;
import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.config.Constant;
import com.baseandroid.config.Global;
import com.baseandroid.mvp.BaseMvpActivity;
import com.baseandroid.retrofit.json.Data;
import com.baseandroid.utils.TimeUtils;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.CashierBean;
import com.qianbao.qianbaobusiness.json.ShopListInfo;

import butterknife.BindView;

public class CashierListActivity extends BaseMvpActivity<ListOfCashierView, CashierListPresenter>
        implements ListOfCashierView, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener
                           , BaseRecycleViewAdapter.OnLoadMoreListener {
    /**
     * 控件部分相关
     */
    @BindView(R.id.toolbar_back)
    RelativeLayout     toolbarBack;     //返回箭头
    @BindView(R.id.toolbar_title)
    TextView           toolbarTitle;          //标题栏
    @BindView(R.id.right_text)
    TextView           rightText;             //条件筛选
    @BindView(R.id.cashier_date)
    TextView           cashierDate;           //
    @BindView(R.id.title_line)
    View               titleLine;
    @BindView(R.id.cashier_before)
    TextView           cashierBefore;         //前一天
    @BindView(R.id.cashier_after)
    TextView           cashierAfter;          //后一天
    @BindView(R.id.rootview)
    LinearLayout       rootview;
    @BindView(R.id.cashier_total)   //总计金额
    TextView           cashierTotal;
    @BindView(R.id.cashier_count)   //总计交易次数
    TextView           cashierCount;
    @BindView(R.id.mrv)     //交易明细列表
    RecyclerView       mrv;
    @BindView(R.id.ptr_ly)  //控制交易列表下拉
    SwipeRefreshLayout ptrLy;
    //    @BindView(R.id.home_icon_empty) //空页面展现
    //    LinearLayout homeIconEmpty;
    @BindView(R.id.home_cashier_fr)
    FrameLayout        homeCashierFr;

    @BindView(R.id.order_status_all)
    RadioButton    orderStatusAll;     //全部状态
    @BindView(R.id.order_status_success)
    RadioButton    orderStatusSuccess;     //交易成功
    @BindView(R.id.order_status_other)
    RadioButton    orderStatusOther;   //其他状态
    @BindView(R.id.order_type_all)
    RadioButton    orderTypeAll;   //全部类型
    @BindView(R.id.order_type_qianbao)
    RadioButton    orderTypeQianbao;   //钱包支付
    @BindView(R.id.order_type_weixin)
    RadioButton    orderTypeWeixin;    //微信支付
    @BindView(R.id.order_type_zhifubao)
    RadioButton    orderTypeZhifubao;  //支付宝支付
    @BindView(R.id.order_type_card)
    RadioButton    orderTypeCard;  //银行卡支付
    @BindView(R.id.selector_cancle)
    TextView       selectorCancle;    //取消
    @BindView(R.id.selector_ok)
    TextView       selectorOk;     //确定
    @BindView(R.id.rel_shaixuan)
    RelativeLayout relShaixuan;

    private BasePopupWindow popupWindow;

    /**
     * 变量相关
     */
    private int offset = 0;    //日期加减操作相关
    private String date;   //选中的日期
    private String typeValue;  //类型
    private String statusValue;    //状态
    private String merchantNos;    //商户编号(商户编号，多个用逗号隔开)
    private int    pageSize;     //每页加载数量
    private String pageNum;   //页码


    /**
     * 适配器相关
     */
    private CashierAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cashier_list;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        toolbarTitle.setText(getApplicationContext().getResources()
                                     .getString(R.string.cashier_title));
        toolbarBack.setOnClickListener(this);
        rightText.setVisibility(View.VISIBLE);
        rightText.setText(getApplicationContext().getResources()
                                  .getString(R.string.cashier_text_selector));
        rightText.setOnClickListener(this);
        rightText.setTextColor(getApplication().getResources().getColor(R.color.text_support));
        date = TimeUtils.getNowDate(Constant.DATE_YYYY_MM_DD, offset);
        pageNum = Constant.DEFAULT_PAGE_NUM;
        pageSize = Constant.DEFAULT_PAGE_SIZE;
        cashierDate.setText(date);
        cashierDate.setOnClickListener(this);
        cashierBefore.setOnClickListener(this);
        cashierAfter.setOnClickListener(this);
        relShaixuan.setOnClickListener(this);
        orderStatusAll.setOnClickListener(this);
        orderStatusSuccess.setOnClickListener(this);
        orderStatusOther.setOnClickListener(this);
        orderTypeAll.setOnClickListener(this);
        orderTypeQianbao.setOnClickListener(this);
        orderTypeWeixin.setOnClickListener(this);
        orderTypeZhifubao.setOnClickListener(this);
        orderTypeCard.setOnClickListener(this);
        selectorCancle.setOnClickListener(this);
        selectorOk.setOnClickListener(this);
        adapter = new CashierAdapter(this);
        mrv.setLayoutManager(new LinearLayoutManager(this));
        ptrLy.setColorSchemeColors(ContextCompat.getColor(this, R.color.text_red));
        ptrLy.setOnRefreshListener(this);
        mrv.setAdapter(adapter);
        adapter.setOnLoadMoreListener(this, mrv);
        adapter.setEnableLoadMore(true);
        adapter.setEmptyView(R.layout.cashier_empty_view);
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {
        statusValue = Constant.ORDER_STATUS_ALL;
        typeValue = Constant.ORDER_TYPE_ALL;
        //        merchantNos = Global.getShopListInfo().getMerchantNos() ;
        ShopListInfo selectedShopListInfo = Global.getSelectedShopListInfo();
        for (int i = 0; i < selectedShopListInfo.merchantList.size(); i++) {
            if (selectedShopListInfo.merchantList.get(i).isSelected) {
                if (TextUtils.isEmpty(merchantNos)) {
                    merchantNos = selectedShopListInfo.merchantList.get(i).merchantNo;
                } else {
                    merchantNos = merchantNos + "," + selectedShopListInfo.merchantList.get(i).merchantNo;
                }
            }
        }
        getPresenter().getOrderList(merchantNos
                , TimeUtils.getDate(date, Constant.DATE_YYYY_MM_DD, Constant.DATE_YYYYMMDD)
                , statusValue, typeValue, pageNum, pageSize + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_back:    //返回
                finish();
                break;

            case R.id.right_text:  //筛选
                if (relShaixuan.getVisibility() == View.GONE) {
                    relShaixuan.setVisibility(View.VISIBLE);
                }
                break;

            case R.id.cashier_before:  //前一天
                date = TimeUtils.getNowDate(Constant.DATE_YYYY_MM_DD, --offset);
                cashierDate.setText(date);
                pageNum = Constant.DEFAULT_PAGE_NUM;
                pageSize = Constant.DEFAULT_PAGE_SIZE;
                getPresenter().getOrderList(merchantNos
                        , TimeUtils.getDate(date, Constant.DATE_YYYY_MM_DD, Constant.DATE_YYYYMMDD)
                        , statusValue, typeValue, pageNum, pageSize + "");
                break;

            case R.id.cashier_after:   //后一天
                if (TimeUtils.isToday(cashierDate.getText().toString(), Constant.DATE_YYYY_MM_DD)) {
                    ToastUtils.showLongToast(getApplicationContext().getResources()
                                                     .getString(R.string.date_is_today));
                } else {
                    pageNum = Constant.DEFAULT_PAGE_NUM;
                    pageSize = Constant.DEFAULT_PAGE_SIZE;
                    date = TimeUtils.getNowDate(Constant.DATE_YYYY_MM_DD, ++offset);
                    cashierDate.setText(date);
                    getPresenter().getOrderList(merchantNos
                            , TimeUtils.getDate(date, Constant.DATE_YYYY_MM_DD, Constant.DATE_YYYYMMDD)
                            , statusValue, typeValue, pageNum, pageSize + "");
                }
                break;

            case R.id.order_status_all:       //全部状态
                statusValue = Constant.ORDER_STATUS_ALL;
                break;

            case R.id.order_status_success:    //交易成功
                statusValue = Constant.ORDER_STATUS_SUCCESS;
                break;

            case R.id.order_status_other:  //其他状态
                statusValue = Constant.ORDER_STATUS_OTHER;
                break;

            case R.id.order_type_all:  //全部类型
                typeValue = Constant.ORDER_TYPE_ALL;
                break;

            case R.id.order_type_qianbao:  //钱包支付
                typeValue = Constant.ORDER_TYPE_QIANBAO;
                break;

            case R.id.order_type_weixin:   //微信支付
                typeValue = Constant.ORDER_TYPE_WEIXIN;
                break;

            case R.id.order_type_zhifubao: //支付宝支付
                typeValue = Constant.ORDER_TYPE_ZHIFUBAO;
                break;

            case R.id.order_type_card: //银行卡支付
                typeValue = Constant.ORDER_TYPE_CARD;
                break;

            case R.id.selector_cancle: //取消操作
                relShaixuan.setVisibility(View.GONE);
                break;

            case R.id.selector_ok:     //确认
                relShaixuan.setVisibility(View.GONE);
                pageNum = Constant.DEFAULT_PAGE_NUM;
                getPresenter().getOrderList(merchantNos
                        , TimeUtils.getDate(date, Constant.DATE_YYYY_MM_DD, Constant.DATE_YYYYMMDD)
                        , statusValue, typeValue, pageNum, pageSize + "");
                break;

            case R.id.rel_shaixuan:
                relShaixuan.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void success(Data<CashierBean> cashierBeanData) {
        if (ptrLy.isRefreshing()) {   //若输入刷新的时候，停止刷新动作，并将原列表数据清空
            ptrLy.setRefreshing(false);
        }

        if (cashierBeanData.getResult().getDataList() == null) {
            return;
        }

        if (pageNum.equals(Constant.DEFAULT_PAGE_NUM)) {
            adapter.resetData(cashierBeanData.getResult().getDataList());
            if (cashierBeanData.getResult().getDataList().size() < Constant.DEFAULT_PAGE_SIZE) {
                getWindow().getDecorView().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.loadMoreEnd(true);
                    }
                }, 200);
            } else {
                adapter.loadMoreComplete();
            }
        } else {
            adapter.addData(cashierBeanData.getResult().getDataList());
            if (cashierBeanData.getResult().getDataList().size() < Constant.DEFAULT_PAGE_SIZE) {
                adapter.loadMoreEnd(false);
            } else {
                adapter.loadMoreComplete();
            }
        }
        cashierTotal.setText(getApplicationContext().getResources()
                                     .getString(R.string.real_pay_total) + cashierBeanData.getResult().getTotalAmt()
                                     + getApplicationContext().getResources().getString(R.string.yuan));
        cashierCount.setText(cashierBeanData.getResult().getTotalNum() + getApplicationContext()
                                                                                 .getResources().getString(R.string.count));
        pageNum = cashierBeanData.getResult().getCurrentPage();
    }

    @Override
    public void error() {
        if (ptrLy.isRefreshing()) {
            ptrLy.setRefreshing(false);
        } else {
            adapter.loadMoreFail();
        }
    }


    @Override
    public void onRefresh() {
        pageNum = Constant.DEFAULT_PAGE_NUM;
        getPresenter().getOrderList(merchantNos
                , TimeUtils.getDate(date, Constant.DATE_YYYY_MM_DD, Constant.DATE_YYYYMMDD)
                , statusValue, typeValue, pageNum, pageSize + "");
    }

    @Override
    public void onLoadMore() {
        getPresenter().getOrderList(merchantNos
                , TimeUtils.getDate(date, Constant.DATE_YYYY_MM_DD, Constant.DATE_YYYYMMDD)
                , statusValue, typeValue, pageNum, pageSize + "");
    }
}
